package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.MainRVAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.AddressListBean;
import com.meihua.baoleyi.bean.GoodsBean;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.utils.UrlContants;
import com.meihua.baoleyi.view.CatLoadingView;

import java.net.URLDecoder;

import static com.meihua.baoleyi.utils.UrlContants.HOST;

/**
 * Created by liming  on 2017/11/1.
 */

public class MainWebviewActivity extends BaseActivity {
    int goods_id;
    private WebView webview;
    private CatLoadingView date;
    private boolean showdelte = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_main);
        goods_id =getIntent().getIntExtra("goods_id",0);
        initView();
        if (date == null) {
            date = new CatLoadingView();
        }
    }
    private void initView() {
        webview = (WebView) findViewById(R.id.webview);
        showWebView();
    }
    private void showWebView() {
        webview.setWebViewClient(new WebViewClient() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (date.isAdded()){
                    date.dismiss();
                }else if (showdelte){
                    date.show(getSupportFragmentManager(), "");
                    showdelte = false;
                }
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (date !=null){
                    date.dismiss();
                }
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDefaultTextEncodingName("utf-8");
        String token=myApp.getToken().toString();
        String userid = myApp.getUser_id();
        webview.loadUrl("http://10.0.2.132:5555/#/goodsDetail/"+token+"/"+userid+"/"+goods_id);
    }
}
